package pearson.automation.forms.courseDetails;

import java.util.ArrayList;

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;

import pearson.automation.forms.ActivityStreamForm;
import pearson.automation.forms.BaseFormWithSidebars;
import pearson.automation.forms.ModuleContents;

/**
 * Form for general course management. All additional functionality is
 * implemented in the pearson.automation.forms.courseDetails package
 * 
 */
public class CourseForm extends BaseFormWithSidebars {

    private Button save = new Button(By.linkText("Save"), "Save");
    private Button settings = new Button(By.linkText("Settings"), "Settings");
    private Button edit = new Button(By.linkText("Edit"), "Edit");
    private Button courseNew = new Button(By.cssSelector("a[title='New element']"), "New");
    private Button courseEdit = new Button(By.cssSelector("a[title='Edit table of contents']"), "Edit course");
    private Button courseDone = new Button(By.cssSelector("section#edit-nav a[title='Done']"), "Done");
    private Button newSection = new Button(By.linkText("New Section"), "New Section");
    private Button newModule = new Button(By.linkText("New Module"), "New Module");
    private Button preview = new Button(By.linkText("Preview"), "Preview");
    private Button done = new Button(By.linkText("Done"), "Done");
    private Label icon = new Label(By.xpath("//a[contains(@class, 'selected')]/span[contains(@class, 'pubstatus')]"),
        "Icon");
    private Button activityStream = new Button(By.linkText("View activity stream"), "Activity stream");

    private ElementList modules = new ElementList(By.xpath("//ol[contains(@class,'course-sortable-modules')]"),
        "Sections");
    private ElementList moduleTitles = new ElementList(By.xpath("//h2[@class='module-title']"), "Sections");

    public CourseForm() {
    super(By.className("course-module"), "Course");
    }

    public CourseSettings clickSettings() {
    settings.clickAndWait();
    return new CourseSettings();
    }

    public CoursePreview clickPreview() {
    preview.clickAndWait();
    return new CoursePreview();
    }

    public boolean isCourseHasPublishingIcon() {
    return (icon.getText().equals("(In Process)"));
    }

    public boolean hasModuleWithName(String moduleName) {
    return modules.hasListElement(moduleName);
    }

    public ModuleContents expandModule(String module) {
    modules.selectListElement(module);
    return new ModuleContents(module);
    }

    public ArrayList<String> getModulesList() {
    ArrayList<String> result = new ArrayList<String>();
    for (WebElement el : moduleTitles.getListElements()) {
        result.add(el.getText());
    }
    return result;
    }

    public void clickEdit() {
    edit.clickAndWait();
    }

    public void clickCourseEdit() {
    courseEdit.click();
    }

    public void clickNew() {
    courseNew.click();
    }

    public void clickDone() {
    done.clickAndWaitNoNavigation();
    }

    public void clickCourseDone() {
    courseDone.clickAndWaitNoNavigation();
    }

    public void addNewSection() {
    courseNew.click();
    newSection.clickAndWait();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    }

    public void addNewModule() {
    courseNew.click();
    newModule.clickAndWait();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    }

    public void assertButtonNewModuleEnabled() {
    courseNew.click();
    Assert.assertTrue(newModule.isEnabled(), "Button 'New Module' is disabled");
    browser.getDriver().getMouse().mouseMove(newModule.getElement().getCoordinates());
    browser.getDriver().getMouse().mouseMove(newModule.getElement().getCoordinates(), -5, -5);

    }

    public void assertButtonNewModuleDisabled() {
    courseNew.click();
    Assert.assertFalse(newModule.isEnabled(), "Button 'New Module' isn't disabled");
    browser.getDriver().getMouse().mouseMove(newModule.getElement().getCoordinates());
    browser.getDriver().getMouse().mouseMove(newModule.getElement().getCoordinates(), -5, -5);
    }

    public void assertButtonEditDisabled() {
    Assert.assertFalse(courseEdit.isEnabled(), "Button 'Edit' isn't disabled");

    }

    public void assertButtonNewSectionDisabled() {
    courseNew.click();
    Assert.assertFalse(newModule.isEnabled(), "Button 'New Module' isn't disabled");
    browser.getDriver().getMouse().mouseMove(newModule.getElement().getCoordinates());
    browser.getDriver().getMouse().mouseMove(newModule.getElement().getCoordinates(), -5, -5);

    }
    
    public ActivityStreamForm clickCourseActivityStream() {
        activityStream.clickAndWait();
        return new ActivityStreamForm();
    }
}
